import os
import pandas as pd
import matplotlib.pyplot as plt

# 文件路径
file_path ='./20180426/'

# 获取file_dir路径下，所有CSV文件文件名
def get_all_file(file_dir):
    L=[]   
    for root, dirs, files in os.walk(file_dir):  
        for file in files:
            if os.path.splitext(file)[1] == '.csv':  
                L.append(os.path.join(root, file))  
    return L

# 获取file_dir路径下，所有CSV文件中的数据
def read_all_data(file_dir):
    count = 0
    datas = []
    header=''
    files = get_all_file(file_dir)
    for file_name in files:
        with open(file_name,'r') as f:
            #获取第一个文件的表头
            header = f.readline().split(',')
            for line in f:
                data = line.split(',')
                entry={}
                for i,value in enumerate(data):
                    if header[i].strip() == 'ID' or header[i].strip()=='Time' or header[i].strip()=='Index':
                        entry[header[i].strip()] = int(value.strip())
                    else:
                        entry[header[i].strip()] = float(value.strip())
                if float(entry['LeftUncaliDirection_Z']) == 1 and float(entry['RightUncaliDirection_Z']) == 1:
                    count+=1
                    entry['ID'] = count
                    datas.append(entry)
    return datas
# 读取并合并数据
data_list = read_all_data(file_path)
dataframe = pd.DataFrame(data_list)
dataframe.to_csv('RecordingData.csv',index=False,sep=',')
print('成功合并',data_list.__len__(),'条数据')

# 分析数据
x=[]
y=[]
for item in data_list:
    if item['Index'] == 0:
        x.append(item['PlaneDeepth'])
        y.append(item['LeftUncaliDirection_X']-item['RightUncaliDirection_X'])

fig = plt.figure(0)
plt.xlabel('Deepth')
plt.ylabel('Left_X - Ritht_X')
plt.scatter(x, y, c='red', alpha=1, marker='+', label='') 
plt.grid(True)
plt.show()